﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using MagicControls;

namespace MagicControls
{
    public abstract class ControlBase : Control, IMagicControl
    {
        #region Public Methods

        public ControlBase()
        {
            //the control template must be in an embeded resource - find it
            Stream resourceStream = null;
            //if the assembly is built with VS there will be a prefix before
            //the resource name we expect. The resource name will be at the 
            //end after a dot
            string dotResource = '.' + ResourceName;
            Assembly assembly = this.GetType().Assembly;
            string[] names = assembly.GetManifestResourceNames();
            foreach (string name in names)
            {
                if (name.Equals(ResourceName) || name.EndsWith(dotResource))
                {
                    resourceStream = assembly.GetManifestResourceStream(name);
                    break;
                }
            }
            Debug.Assert(resourceStream != null, "the resource template" + ResourceName + " not found");
            StreamReader sr = new StreamReader(resourceStream);
            string xaml = sr.ReadToEnd();
            actualControl = InitializeFromXaml(xaml);
            sr.Close();
            Debug.Assert(actualControl != null, "failed to initialize the control");

            base.Width = actualControl.Width;
            base.Height = actualControl.Height;

            Loaded += OnLoaded;
        }

        //forwards FindName to the actual control
        public new DependencyObject FindName(string name)
        {
            return actualControl.FindName(name);
        }

        #endregion Public Methods

        #region Public Properties

        public event SizeChangedEventHandler SizeChanged;

        // Sets/gets the Width of the actual control
        public virtual new double Width
        {
            get { return actualControl.Width; }
            set
            {
                base.Width = actualControl.Width = value;
                if (SizeChanged != null)
                    SizeChanged(this, new SizeChangedEventArg(base.Width, base.Height));
            }
        }

        // Sets/gets the Height of the actual control
        public virtual new double Height
        {
            get { return actualControl.Height; }
            set
            {
                base.Height = actualControl.Height = value;
                if (SizeChanged != null)
                    SizeChanged(this, new SizeChangedEventArg(base.Width, base.Height));
            }
        }

        public virtual Point Position
        {
            get
            {
                return new Point((double) GetValue(Canvas.LeftProperty), (double) GetValue(Canvas.TopProperty));
            }

            set
            {
                SetValue(Canvas.LeftProperty, value.X);
                SetValue(Canvas.TopProperty, value.Y);
            }
        }



        #endregion Public Properties

        #region Public Events

        // Fired when the mouse leaves root object
        public event EventHandler RootLeave;

        #endregion Public Events

        #region Protected Methods

        // Finds the root and registers for its MouseLeave
        protected virtual void OnLoaded(object sender, EventArgs args)
        {
            Height = base.Height;
            Width = base.Width;
        }

        #endregion Protected Methods

        #region Protected Proiperties

        // The resource name used to initialize the actual object
        protected abstract string ResourceName { get; }

        // sometimes this one neds to be modified
        protected FrameworkElement ActualControl
        {
            get { return actualControl; }
        }

        #endregion Protected Proiperties

        #region Data

        private FrameworkElement actualControl = null;

        #endregion Data
    }
}